package com.tfswx.ruleengine.fn;

import com.greenpineyu.fel.context.FelContext;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通过居民身份证号获取出生日期
 *
 * @author Ricky
 * @date 2021/3/25
 */
public class GetCsrqByJmsfzh extends BaseFn {
    private static final Pattern SFZH_PATTERN = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");
    private static final int JMSFZH_2TH_LENGTH = 18;
    private static final int JMSFZH_1TH_LENGTH = 15;

    @Override
    public Object call(Object[] arguments, FelContext context) {
        String jmsfzh = String.valueOf(arguments[0]);
        String csrq = "";
        if (jmsfzh.length() == JMSFZH_2TH_LENGTH) {
            String substring = jmsfzh.substring(6, 6 + 8);
            Matcher matcher = SFZH_PATTERN.matcher(substring);
            if (matcher.find()) {
                csrq = matcher.group(1) + "-" + matcher.group(2) + "-" + matcher.group(3);
            }

        } else if (jmsfzh.length() == JMSFZH_1TH_LENGTH) {
            String substring = jmsfzh.substring(6, 6 + 6);
            Matcher matcher = SFZH_PATTERN.matcher(substring);
            if (matcher.find()) {
                csrq = "19" + matcher.group(1) + "-" + matcher.group(2) + "-" + matcher.group(3);
            }
        }
        return csrq;
    }

    @Override
    public String getZwmc() {
        return "通过居民身份证号获取出生日期";
    }


    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("param1"));
        return params;
    }

    @Override
    public String getName() {
        return "getCsrqByJmsfzh";
    }

}
